/*
 *   AD数据
 */

using System;
using System.Collections.Generic;
using System.Linq;
using SimpleJson;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Dolls
{
	public class UiAdData
	{
		public int    Index         { get; set; }
		public string Name          { get; set; }
		public string Description   { get; set; }
		public long   RewardId1     { get; set; }
		public int    RewardAmount1 { get; set; }
		public long   RewardId2     { get; set; }
		public int    RewardAmount2 { get; set; }
		public long   RewardId3     { get; set; }
		public int    RewardAmount3 { get; set; }
	}

	public class DisplayedHistory
	{
		public ISet<long> DisplayedIndexSet { get; } = new HashSet<long>();
	}

	public class UiAdDataManager : cDataBase<UiAdData, UiAdDataManager>
	{
		private List<long> AvailableIndexList { get; set; } = new();

		/// <summary>
		/// 获取下一个随机显示的索引，并将索引从可用索引中剔除。所有可用索引被使用后重置可用索引
		/// </summary>
		/// <returns></returns>
		public long NextRandomIndex()
		{
			var  count = AvailableIndexList.Count;
			long index;
			switch (count)
			{
				case 0:
					return 0;
				case 1:
					index = AvailableIndexList[0];
					Refresh();
					return index;
				default:
					var i = Random.Range(0, count);
					index = AvailableIndexList[i];
					AvailableIndexList.RemoveAt(i);
					return index;
			}
		}

		public void SavePrefs()
		{
			var history = new DisplayedHistory();
			foreach (var index in mDict.Keys.Where(index => ! AvailableIndexList.Contains(index)))
			{
				history.DisplayedIndexSet.Add(index);
			}
			var json = SimpleJson.SimpleJson.SerializeObject(history);
			PlayerPrefs.SetString(UiAdConstant.序列化键, json);
		}

		public void LoadPrefs()
		{
			var json = PlayerPrefs.GetString(UiAdConstant.序列化键, null);
			if (json == null)
				return;
			var history = SimpleJson.SimpleJson.DeserializeObject<DisplayedHistory>(json);
			Refresh();
			AvailableIndexList.RemoveAll(index => history.DisplayedIndexSet.Contains(index));
		}

		protected override string GetPath()
		{
			return "Assets/project/res/ADHistory.json";
		}

		protected override void ReadData(JsonArray array)
		{
			foreach (var jsonObject in array.Cast<JsonObject>())
			{
				UiAdData data = new()
				{
					Index         = Convert.ToInt32(jsonObject[UiAdConstant.序号]),
					Name          = Convert.ToString(jsonObject[UiAdConstant.名称]),
					Description   = Convert.ToString(jsonObject[UiAdConstant.描述]),
					RewardId1     = Convert.ToInt32(jsonObject[UiAdConstant.奖品id1]),
					RewardAmount1 = Convert.ToInt32(jsonObject[UiAdConstant.奖品数量1]),
					RewardId2     = Convert.ToInt32(jsonObject[UiAdConstant.奖品id2]),
					RewardAmount2 = Convert.ToInt32(jsonObject[UiAdConstant.奖品数量2]),
					RewardId3     = Convert.ToInt32(jsonObject[UiAdConstant.奖品id3]),
					RewardAmount3 = Convert.ToInt32(jsonObject[UiAdConstant.奖品数量3])
				};
				mDict.Add(data.Index, data);
			}
			Refresh();
			gameUtility.Log("UiAd数据读取完成");
		}

		private void Refresh()
		{
			AvailableIndexList = new List<long>(mDict.Keys);
		}
	}
}